package router

import (
	"admin-system-go/model"
	"admin-system-go/model/dto"
	"admin-system-go/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ResourceRouter struct {
	resourceService service.ResourceService
}

func NewResourceRouter() ResourceRouter {
	return ResourceRouter{resourceService: service.GetResourceService()}
}

func (ResourceRouter ResourceRouter) Register(r *gin.Engine) {
	r.GET(apiVersion+"/resource/list", ResourceRouter.QueryResource())
	r.POST(apiVersion+"/resource/create", ResourceRouter.createResource())
	r.POST(apiVersion+"/resource/delete", ResourceRouter.deleteResource())
}

func (ResourceRouter ResourceRouter) QueryResource() gin.HandlerFunc {
	return func(context *gin.Context) {
		var resourceQueryDTO dto.ResourceQueryDTO
		err := context.ShouldBind(&resourceQueryDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, ResourceRouter.resourceService.QueryResource(resourceQueryDTO))
	}
}

func (ResourceRouter ResourceRouter) createResource() gin.HandlerFunc {
	return func(context *gin.Context) {
		var resourceBaseDTO dto.ResourceBaseDTO
		err := context.ShouldBind(&resourceBaseDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, ResourceRouter.resourceService.CreateResource(resourceBaseDTO))
	}
}

func (ResourceRouter ResourceRouter) deleteResource() gin.HandlerFunc {
	return func(context *gin.Context) {
		var resourceDelDTO dto.ResourceDelDTO
		err := context.ShouldBind(&resourceDelDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, ResourceRouter.resourceService.DelResource(resourceDelDTO))
	}
}
